using System;
using System.IO;

namespace SeguradoraIA.Framework.Log.DefaultLoggers
{
    public class TextLogger : ILogger, IDisposable
    {

        private readonly FileInfo arquivo;

        public bool ListenLogMessage { get; set; }
        public bool ListenLogReturn { get; set; }
        public bool ListenLogSQL { get; set; }
        public bool ListenLogEntry { get; set; }
        public bool ListenLogException { get; set; }

        public TextLogger(string fileName)
        {
            arquivo = new FileInfo(fileName);
        }

        public void Log(LogData logData)
        {
            if (!logData.ShouldLog(ListenLogMessage, ListenLogReturn, ListenLogSQL, ListenLogEntry, ListenLogException))
                return;

            using (FileStream reader = arquivo.Open(FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
            using (StreamWriter fs = new StreamWriter(reader))
            {
                FormatMessage(logData, fs);
            }

        }

        private static void FormatMessage(LogData logData, TextWriter fs)
        {
            fs.Write("Thread: ");
            fs.WriteLine(logData.ContextThread.GetHashCode());

            fs.Write("Data: ");
            fs.WriteLine(logData.CreationDate.ToString("dd/MM/yyyy HH:mm:ss:ffff"));

            fs.Write("Linha: ");
            fs.WriteLine(logData.Line);

            fs.Write("Arquivo: ");
            fs.WriteLine(logData.FileName);

            fs.Write("Metodo: ");
            fs.Write(logData.Method);
            fs.Write(" (");
            fs.Write(logData.ParametersDeclaration);
            fs.WriteLine(")");

            fs.WriteLine(logData.Message);

            fs.WriteLine(logData.Information);

            fs.WriteLine("----------------------------------------------------------------------------------");
        }

        public void Dispose()
        {
        }
    }
}